package com.wangyf.thread2;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: wangyf
 * @date: 2021/7/19 20:20
 */
public class T08_HowToStopThread_volatile {

    private static volatile boolean running = true;

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            while (running) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                    System.out.println("go on");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        });
        t.start();

        TimeUnit.SECONDS.sleep(5);

        running = false;
    }
}
